package com.gdupt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gdupt.entity.CustomerLoss;
import com.gdupt.entity.vo.ResponseData;
import com.gdupt.entity.vo.StatisticalReportVo;
import com.gdupt.service.ICustomerLossService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 客户流失 客户流失 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@RestController
@RequestMapping("/customer-loss")
public class CustomerLossController {

    @Autowired
    private ICustomerLossService lossService;

//    //单输入框模糊查询
//    @GetMapping("/search")
//    public ResponseData losePage(@RequestParam(value = "search",required = false) String search,
//                             @RequestParam("page") Integer pageIndex,
//                             @RequestParam("limit") Integer pageSize) throws JsonProcessingException {
//
//        boolean flg=lossService.addRecord();
//        String msg=null;
//        if(flg){
//            msg="你有新的流失记录！！！";
//        }
//        Page page = new Page(pageIndex,pageSize);
//        QueryWrapper<CustomerLoss> qw = null;
//        if(Objects.nonNull(search)){
//            qw = new QueryWrapper<>();
//            qw.like("customer_name",search).or() .like("customer_mgr",search);
//        }
//        IPage list = lossService.page(page, qw);
//        return ResponseData.okPage2(list.getTotal(),list.getRecords(),msg);
//    }

    // 暂缓流失
    @PostMapping("/save-delay")
    public ResponseData delayLoss(CustomerLoss loss){
        System.out.println(loss);
        Integer sta=0;
        loss.setStatus(sta);

        boolean flg=lossService.saveOrUpdate(loss);
        if(flg){
            return ResponseData.okMsg("保存成功！！");
        }
        return ResponseData.failMsg("保存失败！！");
    }

    //确认流失
    @PostMapping("/save-loss")
    public ResponseData determinLoss(CustomerLoss loss){
        System.out.println(loss);
        Integer sta=1;
        loss.setStatus(sta);

        LocalDateTime d = LocalDateTime.now();
        loss.setLossTime(d);

        boolean flg=lossService.saveOrUpdate(loss);
        if(flg){
            return ResponseData.okMsg("保存成功！！");
        }
        return ResponseData.failMsg("保存失败！！");
    }

    //两个输入框+下拉框筛选查询
    @RequestMapping("/all")
    public ResponseData loseAll(@RequestParam(value = "name",required = false) String name,
                                @RequestParam(value = "mgr",required = false) String mgr,
                                @RequestParam(value = "status",required = false) Integer status,
                                @RequestParam("page") Integer pageIndex,
                                @RequestParam("limit") Integer pageSize) throws JsonProcessingException{
        boolean flg=lossService.addRecord();
//        String msg=null;
//        if(flg){
//            msg="你有新的流失记录！！！";
//        }
        Page page = new Page(pageIndex,pageSize);
        QueryWrapper<CustomerLoss> qw = null;

        if(Objects.nonNull(name)||Objects.nonNull(mgr)||Objects.nonNull(status)){
            qw = new QueryWrapper<>();
            if(status.equals(-1)) {
                qw.like("customer_name", name).and(customerLossQueryWrapper -> customerLossQueryWrapper.like("customer_mgr", mgr));
            }else{
                qw.like("customer_name", name).and(customerLossQueryWrapper -> customerLossQueryWrapper.like("customer_mgr", mgr)).and(customerLossQueryWrapper -> customerLossQueryWrapper.eq("status", status));
            }
        }
        IPage list = lossService.page(page, qw);
        return ResponseData.okPage(list.getTotal(),list.getRecords());
    }
//    public ResponseData loseAll(Page page,@RequestParam(value = "name",required = false) String name,
//                               @RequestParam(value = "mgr",required = false) String mgr,
//                               @RequestParam(value = "status",required = false) Integer status){
//        List<CustomerLoss> list=lossService.lossAll(page,name,mgr,status);
//        return ResponseData.okPage(page.getTotal(),list);
//    }

    // 客户流失分析：查看已经确认流失的客户流失记录,两个输入框筛选查询
    @RequestMapping("/list")
    public ResponseData lossList(@RequestParam(value = "name",required = false) String name,
                              @RequestParam(value = "mgr",required = false) String mgr,
                             @RequestParam("page") Integer pageIndex,
                             @RequestParam("limit") Integer pageSize) throws JsonProcessingException{

        Page page = new Page(pageIndex,pageSize);
        QueryWrapper<CustomerLoss> qw = null;
        qw= new QueryWrapper<>();
        qw.isNotNull("loss_reason");

        if(Objects.nonNull(name)||Objects.nonNull(mgr)){

            qw.like("customer_name", name).and(customerLossQueryWrapper ->  customerLossQueryWrapper.like("customer_mgr", mgr));
       }
        IPage list = lossService.page(page, qw);
        return ResponseData.okPage(list.getTotal(),list.getRecords());
    }

}
